define(
		[ "PDUtilDir/dataService", "PDUtilDir/grid", "PDUtilDir/slidebar", "PDUtilDir/util" ],
		function(DataService, Grid, Slidebar, Util) {
			function selectVisitorSupportInit($scope, $compile) {
				return new selectVisitorSupport($scope, $compile);
			}
			function selectVisitorSupport($scope, $compile) {
				this.$scope = $scope;
				this.$compile = $compile;
			}
			selectVisitorSupport.fn = selectVisitorSupport.prototype;
			selectVisitorSupport.fn.init = function(seatId, carNumber, number) {
				this.entity = {
					visitors : undefined,
				};
				this.$scope.bind.seat = {
						seatId: seatId,
						carNumber: carNumber,
						seatNumber: number
				};
			}
			selectVisitorSupport.fn.digest = function() {
				this.$scope.$digest();
			}
			selectVisitorSupport.fn.run = function(seatId, carNumber, number, isReturn) {
				var _this = this;
				if(isReturn == true)
					return;
				_this.init(seatId, carNumber, number);
				_this.slidebars = Slidebar({
					size : 'lg',
					url : getServer() + "/static/app/product/reserveInfos/views/selectVisitor.html",
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function() {
						_this.initialization();
					}
				});
			};
			selectVisitorSupport.fn.initialization = function() {
				var _this = this;
				_this.grid = Grid({
					id : "VisitorForSelectGrid",
					multi : false,
					pagination : false,
					cache : false,
					onSeletedChanged : function(data) {
						if (data.length == 0) {
							_this.entity.visitors = new Array();
							_this.$scope.selectVisitorForm.visitorIds.$setViewValue(undefined);
							return;
						}
						_this.$scope.selectVisitorForm.visitorIds.$setViewValue(true);
						_this.entity.visitors = data;
					},
					layout : [ {
                        name : "站位",
                        field : "isSelected",
                        width: "60px",
                        sort : true,
                        format : function(e) {
                            if (e.row.isSelected)
                                return '<span style="text-align: center;background-color: red;display: inline-block;width: 100%;height: 100%;">已站位</span>';
                            else
                                return '<span style="text-align: center;display: inline-block;width: 100%;height: 100%;">未站位</span>';
                        }
                    }, {
                        name : "中文名",
                        field : "zhName",
                        width: "60px",
                        sort : true
                    }, {
						name : "英文名",
						field : "enName",
						width: "60px",
						sort : true
					}, {
						name : "性别",
						field : "sex",
						width: "40px",
						format : function(e) {
							if (e.row.sex == "1" || e.row.sex == 1)
								return '男';
							else
								return '女';
						}
					}, {
						name : "年龄",
						width: "40px",
						field : "age"
					}, {
						name : "联系电话",
						width: "80px",
						field : "phone"
					}, {
						name : "证件类型",
						field : "certificates",
						width: "60px",
						format:function(e){
							return _this.$scope.cache.certificatesObject.get(e.row.certificates);
						}
					}, {
						name : "证件号码",
						field : "certificatesNumber"
					} ],
					toolbar : [ ],
					data : _this.$scope.cache.visitor
				});
			};
			selectVisitorSupport.fn.save = function() {
                var _this = this,
					data = _this.entity.visitors[0],
					selecteds = _this.$scope.cache.seats,
                    len = selecteds.length,
                    i = 0;

                for(i = 0; i < len; i++){
                    if (data.id == selecteds[i].id){
                        break;
                    }
                }

                if(i < len && len > 0)
                    Util.confirm("游客 : " + selecteds[i].zhName + "(" + selecteds[i].enName + ")从" + selecteds[i].carNumber + "车" + selecteds[i].seatNumber + "号,改成" + _this.$scope.bind.seat.carNumber + "车" + _this.$scope.bind.seat.seatNumber + "号?", function() {
                        DataService.setObject('product.search.reserve.visitors.seat', _this.entity.visitors);
                        _this.$scope.reloadSeatSupport.run();
                        _this.slidebars.close();
                    });
                else {
                    DataService.setObject('product.search.reserve.visitors.seat', _this.entity.visitors);
                    _this.$scope.reloadSeatSupport.run();
                    _this.refreshVisitor(selecteds[i].id);
                    _this.slidebars.close();
				}
			};

            selectVisitorSupport.fn.refreshVisitor = function(id) {
                var data = this.$scope.cache.visitor,
                    len = data.length;

                for(var i = 0; i < len; i++){
                    if (data[i].id == id){
                        data[i].isSelected = true;
                        break;
                    }
                }
            };
			return selectVisitorSupportInit;
		});